package cn.donting.jme.swing;

import com.formdev.flatlaf.extras.components.FlatTree;
import com.formdev.flatlaf.ui.FlatTreeUI;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreePath;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 拖动时不选中 节点
 */
@Slf4j
public class JDragTree extends FlatTree {

    private JFlatTreeUI flatTreeUI;
    /**
     * 拖动的节点
     */
    private TreePath dragPath;

    /**
     * 双击展开节点
     */
    private boolean dbClickExpanded;


    public JDragTree() {
        MouseListener mouseListener = new MouseListener();
        addMouseListener(mouseListener);
        //移除默认的 鼠标事件
        java.awt.event.MouseListener[] mouseListeners = getMouseListeners();
        removeMouseListener(mouseListeners[0]);
        flatTreeUI = new JFlatTreeUI();
        setUI(flatTreeUI);
        mouseListeners = getMouseListeners();
        removeMouseListener(mouseListeners[1]);
        setDragEnabled(true);

        addMouseMotionListener(mouseListener);


    }

    public void setDbClickExpanded(boolean dbClickExpanded) {
        this.dbClickExpanded = dbClickExpanded;
    }

    public final TreePath getDragPath() {
        return dragPath;
    }

    private class MouseListener extends MouseAdapter {

        @Override
        public void mouseDragged(MouseEvent e) {
            TransferHandler transferHandler = JDragTree.this.getTransferHandler();
            if (transferHandler != null) {
                // 获取点击的路径
                TreePath path = JDragTree.this.getClosestPathForLocation(e.getX(), e.getY());
                if (path == null) {
                    dragPath = null;
                    return;
                }
                dragPath = path;
                transferHandler.exportAsDrag((JComponent) e.getSource(), e, TransferHandler.LINK);
                log.debug("mouseDragged:" + path.getLastPathComponent());
            }
            dragPath = null;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // 获取点击的路径
            TreePath path = JDragTree.this.getClosestPathForLocation(e.getX(), e.getY());
            if (path == null) {
                return;
            }
            requestFocus();
            if (e.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(e)) {
                if (flatTreeUI.isLocationInExpandControl(path, e.getX(), e.getY())) {
                    setExpandedState(path, !isExpanded(path));
                } else {
                    setSelectionPath(path);
                }
                return;
            }
            if (dbClickExpanded && e.getClickCount() % 2 == 0 && SwingUtilities.isLeftMouseButton(e)) {
                setExpandedState(path, !isExpanded(path));
                setSelectionPath(path);
                return;
            }
            //右键
            if (SwingUtilities.isRightMouseButton(e)) {
                setSelectionPath(path);
                return;
            }
        }
    }


    private class JFlatTreeUI extends FlatTreeUI {

        @Override
        public boolean isLocationInExpandControl(TreePath path, int mouseX, int mouseY) {
            return super.isLocationInExpandControl(path, mouseX, mouseY);
        }
    }
}
